package cn.kais.drawer.bean;

import cn.kais.drawer.UnitUtil;
import cn.kais.drawer.menu.MenuCodeBarFormat;
import cn.kais.drawer.unit.UnitCode;
import com.alibaba.fastjson.JSONObject;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 二维码
 */
public class BeanCodeQR extends BeanBasic {

    public String code = "123456";

    public int top;
    public int left;
    public int width;
    public int height;

    public Color bColor; // 背景颜色
    public Color mColor; // 主色
    public Color fontColor; // 文本颜色

    public MenuCodeBarFormat format; // 码类型

    public Graphics2D sd; // 绘制

    public BeanCodeQR(Graphics2D sd, JSONObject com) {
        this.sd = sd;
        this.code = com.getString("text");

        this.top = com.getInteger("top");
        this.left = com.getInteger("left");
        this.width = com.getInteger("width");
        this.height = com.getInteger("height");

        this.bColor = UnitUtil.toColor(com.getString("bgColor"));
        this.mColor = UnitUtil.toColor(com.getString("fontColor"));

        this.format = MenuCodeBarFormat.onParseCode(com.getString("encodingType"));

        this.create();
    }

    private void create() {
        // 区域框 测试使用
        if (this.isShowBorder) {
            new BeanBorder(this.sd, this.top, this.left, this.width, this.height);
        }
        sd.setColor(this.bColor);
        sd.fillRect(this.left, this.top, this.width, this.height);
        System.out.println("合并宽度：" + this.width);
        System.out.println("合并高度：" + this.height);

        BufferedImage ima = UnitCode.iCodeBuffer(this.format, this.code, this.width, this.height, this.mColor, this.bColor);
        System.out.println("输出码宽度：" + ima.getWidth());
        System.out.println("输出码高度：" + ima.getHeight());
        sd.drawImage(ima, this.left, this.top, this.width, this.height, null);

    }

}